package com.zyq.support;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicInteger;

public class FileOperation {
    private static final Logger logger = LoggerFactory.getLogger(FileOperation.class);

    public static void moveDir(File[] files, File dir, FileMoveListener listener) {
        AtomicInteger moved = new AtomicInteger();
        final int sumCount = files.length;
        Arrays.stream(files).parallel().forEach(f -> {
            try {
                if (f.exists()) {
                    File dest = new File(dir, f.getName());
                    if (f.isDirectory()) {
                        if (!dest.exists()) {
                            boolean b = f.renameTo(dest);
                            if (!b) FileUtils.moveDirectory(f, dest);
                        } else {
                            logger.info("文件" + dest + "已存在");
                        }
                        listener.move(true, moved.incrementAndGet(), sumCount, f, true);
                    } else {
                        boolean b = f.renameTo(dest);
                        if (!b) FileUtils.moveFile(f, dest);
                        listener.move(true, moved.incrementAndGet(), sumCount, f, false);
                    }
                } else {
                    logger.error("文件不存在:" + f.getAbsolutePath());
                }
            } catch (Exception e) {
                logger.error("move:" + f, e);
            }
        });
    }

    public static void copyDir(File[] files, File dir, FileMoveListener listener) {
        AtomicInteger moved = new AtomicInteger();
        final int sumCount = files.length;
        Arrays.stream(files).parallel().forEach(f -> {
            if (f.exists()) {
                try {
                    if (f.isDirectory()) {
                        File destDir = new File(dir, f.getName());
                        if (!destDir.exists()) {
                            FileUtils.copyDirectory(f, destDir);
                        } else {
                            logger.info("文件" + destDir + "已存在");
                        }
                        listener.move(true, moved.incrementAndGet(), sumCount, f, true);
                    } else {
                        FileUtils.copyFile(f, new File(dir, f.getName()), true);
                        listener.move(true, moved.incrementAndGet(), sumCount, f, false);
                    }
                } catch (IOException e) {
                    listener.move(false, moved.get(), sumCount, f, false);
                    logger.error("拷贝失败:" + f.getAbsolutePath(), e);
                }
            } else {
                logger.error("文件不存在:" + f.getAbsolutePath());
            }
        });
    }

    public static void deleteDir(File[] files, FileMoveListener listener) {
        AtomicInteger deleted = new AtomicInteger();
        final int sumCount = files.length;
        Arrays.stream(files).parallel().forEach((f) -> {
            FileUtils.deleteQuietly(f);
            listener.move(true, deleted.incrementAndGet(), sumCount, f, f.isDirectory());
        });
    }

    @FunctionalInterface
    public interface FileMoveListener {
        void move(boolean isSuccess, int moved, int sumCount, File file, boolean isDir);
    }
}
